package caculate.sliding_window;

/**
 * 计算字符串中最大长度的不重复子串
 *
 * @program: rocsun
 * @description:
 * @author: yx.guo
 * @create: 2025-03-02 23:24
 **/
public class LongestNoRepeatingSubString {

    public static int lengthOfLongestSubstring(String s) {
        if (s == null || s.isEmpty()) return 0;

        char[] charArr = s.toCharArray();
        int n = charArr.length;
        // 字符计数
        int[] cache = new int[128];
        int maxLength = 0;
        int left = 0; // 左指针
        for (int right = 0; right < n; right++) {
            char c = charArr[right];
            // 先放入当前字符
            cache[c]++;

            // 如果当前字符已经存在，表示重复，左窗口右移动
            while (cache[c] > 1) {
                cache[charArr[left]]--;
                left++;
            }

            // 不存在重复字符时，计算长度
            maxLength = Math.max(maxLength, right - left + 1);
        }

        return maxLength;
    }

    public static void main(String[] args) {
        String s = "tmmzuxt";
        System.out.println(lengthOfLongestSubstring(s));
    }

}
